﻿using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Analysis;
using YArchitech.HCLoad.Properties;
using YArchitech.HCLoad.XmlData;
using YArchitech.Revit;

namespace YArchitech.HCLoad.Utils
{
	public class HcUtils
	{
		public static bool DoubleEquals(double x, double y)
		{
			return Math.Abs(x - y) < HcUtils.dZeroPrec;
		}

		public static bool IsEnglish
		{
			get
			{
				return HcUtils.m_bIsEnglish;
			}
			set
			{
				HcUtils.m_bIsEnglish = value;
			}
		}

		public static bool IsZero(double x)
		{
			return Math.Abs(x) < HcUtils.dZeroPrec;
		}

		public static bool IsNum(string pstr)
		{
			return !string.IsNullOrEmpty(pstr) && new Regex("^(\\d+)$").IsMatch(pstr);
		}

		public static int ToInt(string str)
		{
			if (string.IsNullOrEmpty(str))
			{
				return 0;
			}
			return int.Parse(str);
		}

		public static double[] Sum(double[] ary1, double[] ary2)
		{
			int num = ary1.Length;
			int num2 = ary2.Length;
			int num3 = (num > num2) ? num2 : num;
			double[] array = new double[num3];
			for (int i = 0; i < num3; i++)
			{
				array[i] = ary1[i] + ary2[i];
			}
			return array;
		}

		public static double GetMaxValue(double[] aryDoubles)
		{
			double num = aryDoubles[0];
			foreach (double num2 in aryDoubles)
			{
				if (num2 > num)
				{
					num = num2;
				}
			}
			return num;
		}

		public static int GetMaxValueIndex(double[] aryDoubles)
		{
			int num = 0;
			for (int i = 1; i < aryDoubles.Length; i++)
			{
				if (aryDoubles[i] > aryDoubles[num])
				{
					num = i;
				}
			}
			return num;
		}

		public static string ConstructFileFilter(string sFileExternsion)
		{
			return string.Concat(new string[]
			{
				sFileExternsion,
				" files (*.",
				sFileExternsion,
				")|*.",
				sFileExternsion,
				"|All files (*.*)|*.*"
			});
		}

		public static string InstallDirectory
		{
			get
			{
				if (HcUtils.m_sInstallDirectory.Length == 0)
				{
					HcUtils.m_sInstallDirectory = Path.GetDirectoryName(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
				}
				return HcUtils.m_sInstallDirectory;
			}
		}

		public static string SharedParametersFile
		{
			get
			{
				string str = HcUtils.IsEnglish ? HcUtils.sYArchitechSharedParametersFile_En : HcUtils.sYArchitechSharedParametersFile_Cn;
				return HcUtils.InstallDirectory + "\\Data\\" + str;
			}
		}

		public static string SpaceParametersFile
		{
			get
			{
				return HcUtils.InstallDirectory + "\\Data\\" + HcUtils.sYArchitechSpaceParametersFile;
			}
		}

		private static bool TransformFile(string sSourceFilename, string sTargetFilename)
		{
			try
			{
				string fileName = HcUtils.InstallDirectory + "\\Tools\\zlib.exe";
				string arguments = string.Concat(new string[]
				{
					"\"",
					sSourceFilename,
					"\" \"",
					sTargetFilename,
					"\""
				});
				Process process = new Process();
				process.StartInfo = new ProcessStartInfo(fileName, arguments)
				{
					WindowStyle = ProcessWindowStyle.Hidden
				};
				process.StartInfo.CreateNoWindow = true;
				process.StartInfo.UseShellExecute = false;
				process.Start();
				process.WaitForExit();
				return process.HasExited;
			}
			catch (Exception ex)
			{
				string message = ex.Message;
			}
			return false;
		}

		public static bool LoadHclxFile(string sHclxFile, ref DataBase db)
		{
			string tempFileName = Path.GetTempFileName();
			if (!HcUtils.TransformFile(sHclxFile, tempFileName))
			{
				MessageBox.Show(Resources.Error_Transfrom_Failed);
				return false;
			}
			if (!db.LoadXMLFile(tempFileName))
			{
				MessageBox.Show(Resources.Error_LoadFile_Failed);
				return false;
			}
			File.Delete(tempFileName);
			return true;
		}

		public static string HelpFile
		{
			get
			{
				string str = HcUtils.IsEnglish ? HcUtils.sHcHelpFileName_En : HcUtils.sHcHelpFileName_Cn;
				return HcUtils.InstallDirectory + "\\Help\\" + str;
			}
		}

		public static bool ShowHelp()
		{
			Process.Start(HcUtils.HelpFile);
			return true;
		}

		[DllImport("kernel32")]
		private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

		[DllImport("kernel32")]
		private static extern long WritePrivateProfileString(string lpApplicationName, string lpKeyName, string lpString, string lpFileName);

		public static long WriteIniKey(string section, string key, string value, string sFileName)
		{
			if (section.Trim().Length <= 0 || key.Trim().Length <= 0 || value.Trim().Length <= 0)
			{
				return 0L;
			}
			return HcUtils.WritePrivateProfileString(section, key, value, sFileName);
		}

		public static bool OpenHcLoadSPURL()
		{
			int num = 255;
			StringBuilder stringBuilder = new StringBuilder(num);
			if (HcUtils.GetPrivateProfileString("ServicePack", "URL", "", stringBuilder, num, HcUtils.InstallDirectory + "\\Data\\HCLoad.ini") < 0)
			{
				return false;
			}
			Process.Start(stringBuilder.ToString());
			return true;
		}

		public static string GetLocalLanguage()
		{
			int num = 255;
			StringBuilder stringBuilder = new StringBuilder(num);
			if (HcUtils.GetPrivateProfileString("Options", "Language", "", stringBuilder, num, HcUtils.InstallDirectory + "\\Data\\HCLoad.ini") < 0)
			{
				return "";
			}
			return stringBuilder.ToString();
		}

		public static ConstructionType ToConstructionType(HcObjectType hcot)
		{
            ConstructionType result = (ConstructionType)2;
			switch (hcot)
			{
			case HcObjectType.LO_Roof:
            result = (ConstructionType)3;
				break;
			case HcObjectType.LO_SkyLight:
                result = (ConstructionType)9;
				break;
			case HcObjectType.LO_OutWall:
			case HcObjectType.LO_ArcWall:
                result = (ConstructionType)0;
				break;
			case HcObjectType.LO_OutWindow:
                result = (ConstructionType)7;
				break;
			case HcObjectType.LO_InWall:
                result = (ConstructionType)1;
				break;
			case HcObjectType.LO_InWindow:
                result = (ConstructionType)8;
				break;
			case HcObjectType.LO_Stringboard:
                result = (ConstructionType)5;
				break;
			case HcObjectType.LO_OutDoor:
			case HcObjectType.LO_InDoor:
                result = (ConstructionType)6;
				break;
			}
			return result;
		}

		public static string ToConstructionTypeStr(HcObjectType hcot)
		{
			return HcUtils.ToConstructionType(hcot).ToString();
		}

		public static string GetObjectTypeString(Element elemt)
		{
			ElementId typeId = elemt.GetTypeId();
			Element elementById = elemt.Document.GetElementById(typeId);
			if (elementById != null)
			{
				return elementById.Name;
			}
			return string.Empty;
		}

		private static readonly double dZeroPrec = 0.001;

		private static string m_sInstallDirectory = "";

		private static bool m_bIsEnglish = false;

		private static readonly string sYArchitechSharedParametersFile_En = "YArchitech Shared Parameters_EN.txt";

		private static readonly string sYArchitechSharedParametersFile_Cn = "YArchitech Shared Parameters_CN.txt";

		private static readonly string sYArchitechSpaceParametersFile = "YArchitech Space Parameters.xml";

		private static readonly string sHcHelpFileName_En = "HCLoad_EN.chm";

		private static readonly string sHcHelpFileName_Cn = "HCLoad_CN.chm";
	}
}
